package com.example.yubo.zyfypt_403yb.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yubo.zyfypt_403yb.R;
import com.example.yubo.zyfypt_403yb.adapter.MyAdapter;
import com.example.yubo.zyfypt_403yb.adapter.TwareAdapter;
import com.example.yubo.zyfypt_403yb.bean.ArticleBean;
import com.example.yubo.zyfypt_403yb.bean.TwareBean;
import com.example.yubo.zyfypt_403yb.iface.ArticleListener;
import com.example.yubo.zyfypt_403yb.iface.TwareListener;
import com.example.yubo.zyfypt_403yb.model.ArticleModel;
import com.example.yubo.zyfypt_403yb.model.TwareModel;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment3 extends Fragment {
    private TextView tvinfo;
    private View view = null;
    private Context context;
    private int page=1; // 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置
    private SharedPreferences sp;
    private String sessionid;
    private RecyclerView recyclerView;
   // private RecyclerView.LayoutManager layoutManager;//显示布局效果
    private TwareAdapter twareAdapter;//适配器
    private LinearLayoutManager  layoutManager;//显示布局效果

    private List<TwareBean> list = null;  //数据源
    TwareListener twareListener = new TwareListener() {
        public void onResponse(List<TwareBean> beanlist) {
            //list = list;
            if(page==1)
            {
                list=beanlist;
            }
            else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            twareAdapter.setList(list);//得到网络中的数据 传到Adapter
            //然后将twareAdapter中的数据更新
            twareAdapter.notifyDataSetChanged();
        }

        //响应失败
        public void onFail(String msg) {
            Toast.makeText(context, "msg", Toast.LENGTH_SHORT).show();
        }
    };
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        context=getActivity();
        TwareModel twareModel = new TwareModel();
        //动态加载Fragment 的布局文件
        view = inflater.inflate(R.layout.fragment3,container,false);
        //获取布局上的控件 通过 view 获取
        tvinfo= (TextView) view.findViewById(R.id.textView8);
        Toast.makeText(context,"加载Fragment课件",Toast.LENGTH_SHORT);
        //返回动态生成的view
        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();
        initRecycleView();

        //调用TwaseModel中的getResoutList方法  实例化ActivityModel方法 获取网络中的数据
        twareModel.getResoutList("tware",1,
                 sessionid,
                twareListener);
        return view;
    }
    private void readSP() {
        sessionid = sp.getString("sessionid", null);
    }

    private void initRecycleView() {
        //获取RecyclerView 设置属性 获取数据源 绑定
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        //创建默认的线性布局
        layoutManager = new LinearLayoutManager(context);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        //  list=getData();
        twareAdapter = new TwareAdapter(context);
        twareAdapter.setList(list);
        recyclerView.setAdapter(twareAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    TwareModel twareModel = new TwareModel();
                    twareModel.getResoutList("tware",page,
                            sessionid,
                            twareListener);

                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });
    }
}